package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.FinancialStandardValueDAO;
import cn.edu.nju.software.greencreditsystem.business.WoleAnalyser;
import cn.edu.nju.software.greencreditsystem.domain.FinancialFactor;
import cn.edu.nju.software.greencreditsystem.domain.FinancialStandardValue;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-11
 * 类描述：财务指标标准值DAO实现
 */
public class FinancialStandardValueDAOImpl implements FinancialStandardValueDAO
{

    @Override
    public double[] getStandardValues()
    {
        double result[] = new double[WoleAnalyser.factorNames.length];
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            for(int i = 0; i < result.length; i++)
            {
                Query q = s.createQuery("select value from FinancialStandardValue "
                                        + "where factor.name = :n and type = 0");
                q.setString("n", WoleAnalyser.factorNames[i]);
                result[i] = (Double) q.uniqueResult();
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public double getStandardValue(int factorID)
    {
        double result = 0;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = (Double) s.createQuery("select value from FinancialStandardValue "
                                              + "where factor.id = " + factorID
                                              + "and type = 0").uniqueResult();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public double[] getChangeValues()
    {
        double result[] = new double[WoleAnalyser.factorNames.length];
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            for(int i = 0; i < result.length; i++)
            {
                Query q = s.createQuery("select value from FinancialStandardValue "
                                        + "where factor.name = :n and type = 1");
                q.setString("n", WoleAnalyser.factorNames[i]);
                result[i] = (Double) q.uniqueResult();
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public double getChangeValue(int factorID)
    {
        double result = 0;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = (Double) s.createQuery("select value from FinancialStandardValue "
                                              + "where factor.id = " + factorID
                                              + "and type = 1").uniqueResult();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public FinancialStandardValue[] getStandardValueObjects()
    {
        FinancialStandardValue result[] = new FinancialStandardValue[WoleAnalyser.factorNames.length];
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            for(int i = 0; i < result.length; i++)
            {
                Query q = s.createQuery("from FinancialStandardValue "
                                        + "where factor.name = :n and type = 0");
                q.setString("n", WoleAnalyser.factorNames[i]);
                result[i] = (FinancialStandardValue) q.uniqueResult();
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public FinancialStandardValue[] getChangeValueObjects()
    {
        FinancialStandardValue result[] = new FinancialStandardValue[WoleAnalyser.factorNames.length];
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            for(int i = 0; i < result.length; i++)
            {
                Query q = s.createQuery("from FinancialStandardValue "
                                        + "where factor.name = :n and type = 1");
                q.setString("n", WoleAnalyser.factorNames[i]);
                result[i] = (FinancialStandardValue) q.uniqueResult();
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean update(FinancialStandardValue fsv)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.update(fsv);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public FinancialStandardValue getStandardValueObjects(int financialFactorID)
    {
        FinancialStandardValue result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialStandardValue "
                                    + "where factor.id = " + financialFactorID + " and type = 0");
            result = (FinancialStandardValue) q.uniqueResult();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public FinancialStandardValue getChangeValueObjects(int financialFactorID)
    {
        FinancialStandardValue result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialStandardValue "
                                    + "where factor.id = " + financialFactorID + " and type = 1");
            result = (FinancialStandardValue) q.uniqueResult();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }
    
}
